﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace ConfigLession
{
    public partial class Form1 : Form
    {
        ConfigHelper configHelper = new ConfigHelper();
        XMLHelper xmlHelper = new XMLHelper();

        #region 声明读写INI文件的API函数  引用系统自带的 Ken132.dll 

        [DllImport("kernel32.dll")]//win32 dll 对应的命令
        //写配置文件ini 返回0 表示失败的；非0 表示成功 （调用非托管类型的dll）
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        //获取配置文件ini
        [DllImport("kernel32")] //win32 dll 对应的命令
        private static extern long GetPrivateProfileString(string section,string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// 读取ini配置文件
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="NoText"></param>
        /// <param name="iniFilePath"></param>
        /// <returns></returns>
        public static string ReadIni(string Section, string Key, string NoText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                return temp.ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 写ini配置文件
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        /// <param name="iniFilePath"></param>
        /// <returns></returns>
        public static bool WriteIni(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                return WritePrivateProfileString(Section, Key, Value, iniFilePath) != 0;
            }
            return false;
        }

        #endregion

        private string iniPath = Application.StartupPath + "\\Settings\\Port.ini";

        public Form1()
        {
            InitializeComponent();
            //得到AppSettings所有节点
            listBox2.Items.AddRange(configHelper.ReadAppSettings());
            listBox3.Items.AddRange(xmlHelper.ReadMySection("serial"));
            ini_readConfig();
        }

        /// <summary>
        /// 获取按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = configHelper.ReadAppSetting(listBox2.SelectedItem.ToString());
        }


        /// <summary>
        /// 修改按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            configHelper.WriteAppSetting(listBox2.SelectedItem.ToString(), textBox3.Text);
        }

        /// <summary>
        /// 删除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            configHelper.DelAppSetting(listBox2.SelectedItem.ToString());
        }

        /// <summary>
        /// 增加按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            configHelper.AddAppSetting(textBox2.Text, textBox4.Text);
        }

        /// <summary>
        /// XML文件获取按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            //textBox8.Text = xmlHelper.ReadMyNode("serial",listBox3.SelectedItem.ToString());
            textBox8.Text = xmlHelper.ReadMyNode1("serial", listBox3.SelectedItem.ToString());

        }

        /// <summary>
        /// XML文件修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            xmlHelper.WriteMyNode("serial", listBox3.SelectedItem.ToString(), textBox6.Text);
        }

        /// <summary>
        /// XML文件删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            xmlHelper.DelMyNode("serial", listBox3.SelectedItem.ToString());
        }

        /// <summary>
        /// XML文件新增按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            xmlHelper.AddMyNode("serial", textBox7.Text, textBox5.Text);

        }

        private void label1_Click(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// 连接按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            label1.Text = configHelper.ReadconnectionString("sqlserver");
            SqlConnection sqlConnection = new SqlConnection(label1.Text);
            sqlConnection.Open();
            if (sqlConnection.State == ConnectionState.Open)
            {
                label2.Text = "连接成功！";
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// ini写入按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Ini_Save_Click(object sender, EventArgs e)
        {
            bool resule = true;
            resule &= WriteIni("PLC配置", "PlcIpAddress", Txt_ip.Text, iniPath);
            resule &= WriteIni("PLC配置", "PlcPort", Txt_port.Text, iniPath);

            if (resule == true)
            {
                MessageBox.Show("参数保存成功");
            }
            else 
            {
                MessageBox.Show("参数保存失败");
            }

        }

        /// <summary>
        /// ini读取按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Ini_Read_Click(object sender, EventArgs e)
        {
            Txt_ip.Text = ReadIni("PLC配置","PlcIpAddress","",iniPath);
            Txt_port.Text = ReadIni("PLC配置", "PlcPort", "", iniPath);

        }

        /// <summary>
        /// 页面首次加载自动读取ini文件
        /// </summary>
        private void ini_readConfig()
        {
            Txt_ip.Text = ReadIni("PLC配置", "PlcIpAddress", "", iniPath);
            Txt_port.Text = ReadIni("PLC配置", "PlcPort", "", iniPath);
        }


    }
}
